//
//  ModelOrder.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/4.
//
//

#import "ModelOrder.h"

NSString *const kModelOrderStatus = KEY_status;
NSString *const kModelOrderSlogan = KEY_slogan;
NSString *const kModelOrderProductId = KEY_productId;
NSString *const kModelOrderBeforePaymentDetail = KEY_beforePaymentDetail;
NSString *const kModelOrderOrderNo = KEY_orderNo;
NSString *const kModelOrderProductName = KEY_productName;
NSString *const kModelOrderRepaymentDetail = KEY_repaymentDetail;
NSString *const kModelOrderSettledLoanDetail = KEY_settledLoanDetail;

@interface ModelOrder ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelOrder

@synthesize status = _status;
@synthesize slogan = _slogan;
@synthesize productId = _productId;
@synthesize beforePaymentDetail = _beforePaymentDetail;
@synthesize orderNo = _orderNo;
@synthesize productName = _productName;
@synthesize repaymentDetail = _repaymentDetail;
@synthesize settledLoanDetail = _settledLoanDetail;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.status = [self objectOrNilForKey:kModelOrderStatus fromDictionary:dict];
        self.slogan = [self objectOrNilForKey:kModelOrderSlogan fromDictionary:dict];
        self.productId = [[self objectOrNilForKey:kModelOrderProductId fromDictionary:dict] doubleValue];
        self.beforePaymentDetail = [ModelBeforePaymentDetail modelObjectWithDictionary:[dict objectForKey:kModelOrderBeforePaymentDetail]];
        self.orderNo = [self objectOrNilForKey:kModelOrderOrderNo fromDictionary:dict];
        self.productName = [self objectOrNilForKey:kModelOrderProductName fromDictionary:dict];
        self.repaymentDetail = [ModelRepayment modelObjectWithDictionary:[dict objectForKey:kModelOrderRepaymentDetail]];
        self.settledLoanDetail = [ModelRepayment modelObjectWithDictionary:[dict objectForKey:kModelOrderSettledLoanDetail]];
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.status forKey:kModelOrderStatus];
    [mutableDict setValue:self.slogan forKey:kModelOrderSlogan];
    [mutableDict setValue:[NSNumber numberWithDouble:self.productId] forKey:kModelOrderProductId];
    [mutableDict setValue:[self.beforePaymentDetail dictionaryRepresentation] forKey:kModelOrderBeforePaymentDetail];
    [mutableDict setValue:self.orderNo forKey:kModelOrderOrderNo];
    [mutableDict setValue:self.productName forKey:kModelOrderProductName];
    [mutableDict setValue:[self.repaymentDetail dictionaryRepresentation] forKey:kModelOrderRepaymentDetail];
    [mutableDict setValue:[self.settledLoanDetail dictionaryRepresentation] forKey:kModelOrderSettledLoanDetail];
    
    
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
